//
//  ProprertyPayController.m
//  PropertyMananger
//
//  Created by Mansion on 17/2/25.
//  Copyright © 2017年 BH. All rights reserved.
//

#import "ProprertyPayController.h"
#import "SelectedTypeCell.h"
#import "CheckInputCell.h"
#import "ClickQueryCell.h"
#import "QueryResultController.h"
#import "SelectedMenuView.h"
#import "SelectedFloorCell.h"
#import "AddressView.h"

#import "RoomNumController.h"
#import "QueryResultModel.h"
#import "CarNoController.h"
#import "CarNumQueryController.h"

@interface ProprertyPayController ()<SPullDownMenuViewDelegate,SelectedRoomNumDelegate,SelectedCarNumDelegate>
@property (nonatomic,strong) NSArray * titleArr;

@property (nonatomic,assign) NSInteger seletedType;

@property (nonatomic,strong) SelectedMenuView * seletedMenuView;

@property (nonatomic,copy) NSString * adressStr;//选择地址
@property (nonatomic,copy) NSString * timeStr;//选择时间
@property (nonatomic,copy) NSString * BuildUnitId;//楼栋ID
@property (nonatomic,copy) NSString * carNumStr;//车牌号

@property (nonatomic,copy) NSString * roomNum;//房间号
@property (nonatomic,strong) NSMutableArray * propertyPayArray;


@end

@implementation ProprertyPayController

- (void)viewDidLoad {
    [super viewDidLoad];
    
   self.adressStr = @"请选择楼栋";
   self.timeStr = @"请选择缴费时间";
   self.roomNum = @"请选择房间号";
  self.carNumStr = @"请选择车牌号";
    
    [self initTitleViewName:@"缴费查询"];
    
    self.titleArr = @[@[@"物业费",@"停车费"]];
    
    self.propertyPayArray = [[NSMutableArray alloc]init];
    
    [self initTableViewWithView:self.view frame:CGRectMake(0, 50, BHscreenW, BHscreenH - 64 - 50) isGroup:NO];
    
    self.tableView.separatorStyle = NO;
    self.tableView.backgroundColor = BHGrayColor;
    
    self.seletedMenuView = [[SelectedMenuView alloc]initWithFrame:CGRectMake(0, 0, BHscreenW, 50)];
    
    [self.view addSubview:self.seletedMenuView];
    
    self.menu = [[SPullDownMenuView alloc] initWithFrame:CGRectMake(0, 10, BHscreenW, 30) withTitle:self.titleArr withSelectColor:MColor(241, 125,174)];
    self.menu.delegate = self;
    [self.view addSubview:self.menu];
    
}
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        if (self.seletedType == 0) {
            
            return 3;
            
        }else{
            
        return 1;
        }
        
        
    }else{
        
        return 1;
    }
    
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
          
            if (self.seletedType == 0) {
                
                SelectedFloorCell * cell = [SelectedFloorCell cellWithTableView:tableView];

                cell.subTitle.text =  self.adressStr;
                return cell;
                
            }else{
            
                 SelectedTypeCell * cell = [SelectedTypeCell cellWithTableView:tableView];
                cell.titleLabel.text = @"车牌号";
                cell.subTitle.text = self.carNumStr;
                cell.starImg.hidden = NO;
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                
                return cell;
            }
            
        }else if (indexPath.row == 1){
           
            
            if (self.seletedType == 0) {
             
                SelectedTypeCell * cell = [SelectedTypeCell cellWithTableView:tableView];
                cell.titleLabel.text = @"房间号";
                cell.subTitle.text = self.roomNum;

                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                
                return cell;
                
            }
            
            
        }
        else if (indexPath.row == 2){
           
            SelectedTypeCell * cell = [SelectedTypeCell cellWithTableView:tableView];
            cell.titleLabel.text = @"缴费时间";
            cell.subTitle.text = self.timeStr;
          
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.starImg.hidden = YES;
            
            return cell;
            
        }
    }else if (indexPath.section == 1){
        
        ClickQueryCell * cell = [ClickQueryCell cellWithTableView:tableView];
        
        [cell.comfirmBtn addTarget:self action:@selector(queryClick:) forControlEvents:UIControlEventTouchUpInside];
        if (self.seletedType == 1) {
            
            cell.subTitleLabel.text = @"号为必填选项，至少输入一个车牌号或车位号";
        }
        return cell;
        
    }

    return nil;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        
        return 50;
    }else{
        
        return 200;
    }
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    DLog(@"%ld",indexPath.row);
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
            if (self.seletedType == 0) {
                
                AddressView *addressView = [[AddressView alloc] initWithFrame:CGRectMake(0, 0, [UIApplication sharedApplication].keyWindow.frame.size.width, [UIApplication sharedApplication].keyWindow.frame.size.height)];
                addressView.block = ^(NSString *addressStr){
                    
                    DLog(@"选择了 === %@",addressStr);
                    self.adressStr = addressStr;
                    
                    [self.tableView reloadData];
                    
                };
                
                addressView.BuildUnitIDBlock = ^(NSString * BuildUnitId){
                    
                    DLog(@"BuildUnitId===%@",BuildUnitId);
                    self.BuildUnitId = BuildUnitId;
                };
                
                [addressView showView:self.view];
            }else if (self.seletedType == 1){
            
                DLog(@"请选择车牌号");
                CarNoController * roomVC = [[CarNoController alloc]init];
                roomVC.delegate = self;
                [self.navigationController pushViewController:roomVC animated:NO];
                
            }
        }else if (indexPath.row == 1){
            if (self.seletedType == 0) {
                
                if ([self.adressStr isEqualToString:@"请选择楼栋"]) {
                    
                    [MBProgressHUD showError:@"请选择楼栋" time:1.5];
                }else{
                    
                    DLog(@"选择房间号");
                    
                    RoomNumController * roomVC = [[RoomNumController alloc]init];
                    roomVC.delegate = self;
                    roomVC.BuildUnitId = self.BuildUnitId;
                    
                    [self.navigationController pushViewController:roomVC animated:NO];
                    
                }
            }
            
        }else if (indexPath.row == 2){
            
            SelectedTypeCell * cell = [SelectedTypeCell cellWithTableView:tableView];
            __block ProprertyPayController *weakSelf = self;
            dateTimePickerView = [[HcdDateTimePickerView alloc] initWithDatePickerMode:DatePickerYearMonthMode defaultDateTime:[[NSDate alloc]initWithTimeIntervalSinceNow:1000]];
            
            dateTimePickerView.topViewColor = BHGrayColor;
            dateTimePickerView.buttonTitleColor = Theme_Color;
            dateTimePickerView.clickedOkBtn = ^(NSString * datetimeStr){
                NSLog(@"%@", datetimeStr);
                
                NSArray * timeArr = [datetimeStr componentsSeparatedByString:@"-"];
                NSString * monthStr = timeArr[1];
                
                if (monthStr.integerValue <=9) {
                    
                    weakSelf.timeStr = [NSString stringWithFormat:@"%@-0%@",timeArr[0],timeArr[1]];
                    cell.subTitle.text = [NSString stringWithFormat:@"%@-0%@",timeArr[0],timeArr[1]];
                }else{
                
                weakSelf.timeStr = [NSString stringWithFormat:@"%@-%@",timeArr[0],timeArr[1]];
                 cell.subTitle.text = [NSString stringWithFormat:@"%@-%@",timeArr[0],timeArr[1]];
                }
                
            //    weakSelf.timeStr = datetimeStr;
                
                //  weakSelf.publicVC.selectedTimeStr = datetimeStr;
                cell.subTitle.text =  datetimeStr;
                
                [weakSelf.tableView reloadData];
            };
            
            if (dateTimePickerView) {
                [[UIApplication sharedApplication].keyWindow addSubview:dateTimePickerView];
                [dateTimePickerView showHcdDateTimePicker];
            }
            
        }

    }
   
}
#pragma mark - 点击查询
- (void)queryClick:(UIButton *)button{
    
    DLog(@"点击查询");
    if (self.seletedType == 0) {
        
        if ([self.adressStr isEqualToString:@"请选择楼栋"]) {
            
            [MBProgressHUD showError:@"请选择楼栋" toView:self.view time:1.5];
            
        }else if ([self.roomNum isEqualToString:@"请选择房间号"]){
            
            [MBProgressHUD showError:@"请选择房间号" toView:self.view time:1.5];
            
        }else if ([self.timeStr isEqualToString:@"请选择缴费时间"]){
            
            [MBProgressHUD showError:@"请选择缴费时间" toView:self.view time:1.5];
            
        }else{
            
            [self postPropretyPayQuery];
            
        }

    }else if (self.seletedType == 1){
    
        if ([self.carNumStr isEqualToString: @"请选择车牌号"]) {
            
             [MBProgressHUD showError:@"请选择车牌号" toView:self.view time:1.5];
        }else{
        
             [self postCarNumPayQuery];
        }
   
        
    }
   

}
- (void)pullDownMenuView:(SPullDownMenuView *)menu didSelectedIndex:(NSIndexPath *)indexPath{
    
    NSLog(@"=== %ld",indexPath.section);
    self.seletedType = indexPath.section;
   
    [self.tableView reloadData];
    
}
#pragma mark - 实现房号选择代理
- (void)didSelectedRoomNum:(NSString *)roomNum{
    
    DLog(@"%@",roomNum);
    self.roomNum = roomNum;
    
    [self.tableView reloadData];
}
- (void)didSelectedCarNum:(NSString *)carNum{

    self.carNumStr = carNum;
    [self.tableView reloadData];

}

#pragma mark - 获取物业缴费查询
- (void)postPropretyPayQuery{
    
    NSDictionary * parms = nil;
    
    parms = @{
              @"Cid":userModel.CommunityID,
              @"Type":@"1",
              @"ChargeTime":self.timeStr,
              @"BuildUnitId":self.BuildUnitId,
              @"RoomName":self.roomNum,
              @"json":@"\{\"jsonEntity\":\{},\"action\":\"checkpropertybill\",\"version\":\"V1.0\"}"
              };
    
    DLog(@"=== 参数提交 === %@",parms);
    
    [HomeNetWorkRequest postPropretyPayQuerySuccess:^(id data) {
        
        [self.propertyPayArray removeAllObjects];
        
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSString * str = [NSString stringWithFormat:@"%@",dict[@"Data"]];
        NSData * data2 = [str dataUsingEncoding:NSUTF8StringEncoding];
        NSArray * array = [NSJSONSerialization JSONObjectWithData:data2 options:NSJSONReadingMutableContainers error:nil];
        DLog(@"物业缴费数据 == %@",array);
        DLog(@"%@",dict[@"Msg"]);
        if (array.count != 0) {
            
            for (NSDictionary * dict in array) {
                
                QueryResultModel * model = [[QueryResultModel alloc]init];
                
                [model setValuesForKeysWithDictionary:dict];
                
                [self.propertyPayArray addObject:model];
                
                DLog(@"缴费状态 === %@",model.ChargeState);
                
            }
            
            QueryResultController * queryResutlVC = [[QueryResultController alloc]init];
            
            queryResutlVC.propertyArray = self.propertyPayArray;
            queryResutlVC.selectedType = self.seletedType;
            
            [self.navigationController pushViewController:queryResutlVC animated:YES];

        }else{
        
            [MBProgressHUD showError:@"没有查到匹配的记录" time:1.5];
        
        }
        
        [self.tableView reloadData];
        
    } Fail:^(NSString *errorDes) {
        
    } param:parms];
    
}
#pragma mark - 获取车牌号缴费查询
- (void)postCarNumPayQuery{
    
    NSDictionary * parms = nil;
    
    parms = @{
              @"Cid":userModel.CommunityID,
              @"carNo":self.carNumStr,
              @"json":@"\{\"jsonEntity\":\{},\"action\":\"checkcarbill\",\"version\":\"V1.0\"}"
              };
    
    DLog(@"=== 参数提交 === %@",parms);
    
    [HomeNetWorkRequest postCarNumPayQuerySuccess:^(id data) {
        
        [self.propertyPayArray removeAllObjects];
        
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSString * str = [NSString stringWithFormat:@"%@",dict[@"Data"]];
        NSData * data2 = [str dataUsingEncoding:NSUTF8StringEncoding];
        NSArray * array = [NSJSONSerialization JSONObjectWithData:data2 options:NSJSONReadingMutableContainers error:nil];
        DLog(@"车位号缴费数据 == %@",array);
        DLog(@"%@",dict[@"Msg"]);
        if (array.count != 0) {
            
            for (NSDictionary * dict in array) {
                
                QueryResultModel * model = [[QueryResultModel alloc]init];
                
                [model setValuesForKeysWithDictionary:dict];
                
                [self.propertyPayArray addObject:model];
                
                DLog(@"缴费状态 === %@",model.ChargeState);
                
            }
            
            CarNumQueryController * queryResutlVC = [[CarNumQueryController alloc]init];
            
            queryResutlVC.propertyArray = self.propertyPayArray;
            queryResutlVC.selectedType = self.seletedType;
            
            [self.navigationController pushViewController:queryResutlVC animated:YES];
        }else{
            
            [MBProgressHUD showError:@"没有查到匹配的记录" time:1.5];
            
        }
        
        [self.tableView reloadData];
        
    } Fail:^(NSString *errorDes) {
        
    } param:parms];
    
}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];

}


@end
